package com.szqhyxj.youxiangju.biz.partnerHome.equipment.currentRecord;

import com.szqhyxj.youxiangju.BaseConsumer;
import com.szqhyxj.youxiangju.biz.partnerHome.equipment.bean.Record;
import com.szqhyxj.youxiangju.sys.AbBasePresenter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * XiaoPanYu
 * on 2018/11/22
 */
public class CurrentRecordPresenter extends AbBasePresenter implements CurrentRecordContract.Presenter{

    private CurrentRecordContract.View view;
    private List<String> timeList = new ArrayList<>();
    public CurrentRecordPresenter(CurrentRecordContract.View view){
        this.view = view;
    }

    @Override
    public void init(int type){
        if(type == CurrentRecordModel.RECORD_TYPE){
            getRecord();
        }

        if(type == CurrentRecordModel.ACTIVATE_TYPE){
            getRecordActivate();
        }

        if(type == CurrentRecordModel.OUT_TYPE){
            getRecordOut();
        }
    }

    @Override
    public void getRecord(){
        addDisposables(CurrentRecordModel.getRecord()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseConsumer<List<Record>>(){
                    @Override
                    protected void error(String code, String msg){
                        view.showLoading();
                        view.showToast(msg);
                    }

                    @Override
                    protected void successAccept(List<Record> records){
                        view.showLoading();
                        CurrentRecordModel.initResult(timeList,records);
                        view.showCurrentRecord(records,timeList);
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.showLoading();
                        view.showToast(throwable.getMessage());
                    }
                }));
    }

    @Override
    public void getRecordActivate(){
        addDisposables(CurrentRecordModel.getRecordActivate()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseConsumer<List<Record>>(){
                    @Override
                    protected void error(String code, String msg){
                        view.showLoading();
                        view.showToast(msg);
                    }

                    @Override
                    protected void successAccept(List<Record> records){
                        view.showLoading();
                        CurrentRecordModel.initResult(timeList,records);
                        view.showCurrentRecord(records,timeList);
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.showLoading();
                        view.showToast(throwable.getMessage());
                    }
                }));
    }

    @Override
    public void getRecordOut(){
        addDisposables(CurrentRecordModel.getRecordOut()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseConsumer<List<Record>>(){
                    @Override
                    protected void error(String code, String msg){
                        view.showLoading();
                        view.showToast(msg);
                    }

                    @Override
                    protected void successAccept(List<Record> records){
                        view.showLoading();
                        CurrentRecordModel.initResult(timeList,records);
                        view.showCurrentRecord(records,timeList);
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.showLoading();
                        view.showToast(throwable.getMessage());
                    }
                }));
    }

    @Override
    public void addDisposables(Disposable disposable){
        super.addDisposables(disposable);
        view.showLoading();
    }
}
